package com.xmy.solution.service.impl;

import java.util.List;

import com.xmy.common.exception.base.BaseException;
import com.xmy.common.utils.DateUtils;
import com.xmy.common.utils.SecurityUtils;
import com.xmy.solution.mapper.SolutionCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xmy.solution.mapper.CategoriesMapper;
import com.xmy.solution.domain.Categories;
import com.xmy.solution.service.IcategoriesService;

/**
 * 方案分类Service业务层处理
 *
 * @author 夏明毅
 * @date 2024-12-28
 */
@Service
public class categoriesServiceImpl implements IcategoriesService {
    @Autowired
    private CategoriesMapper categoriesMapper;
    @Autowired
    private SolutionCategoryMapper solutionCategoryMapper;

    /**
     * 查询方案分类
     *
     * @param id 方案分类主键
     * @return 方案分类
     */
    @Override
    public Categories selectcategoriesById(Long id) {
        return categoriesMapper.selectCategoriesById(id);
    }

    /**
     * 查询方案分类列表
     *
     * @param categories 方案分类
     * @return 方案分类
     */
    @Override
    public List<Categories> selectcategoriesList(Categories categories) {
        return categoriesMapper.selectCategoriesList(categories);
    }

    /**
     * 新增方案分类
     *
     * @param categories 方案分类
     * @return 结果
     */
    @Override
    public int insertCategories(Categories categories) {
        categories.setCreateTime(DateUtils.getNowDate());
        categories.setCreateBy(SecurityUtils.getUsername());
        categories.setStatus(0l);
        return categoriesMapper.insertCategories(categories);
    }

    /**
     * 修改方案分类
     *
     * @param categories 方案分类
     * @return 结果
     */
    @Override
    public int updatecategories(Categories categories) {
        checkCategoryHasSolutions(new Long[] { categories.getId() });
        categories.setUpdateTime(DateUtils.getNowDate());
        categories.setUpdateBy(SecurityUtils.getUsername());
        return categoriesMapper.updateCategories(categories);
    }

    /**
     * 批量删除方案分类
     *
     * @param ids 需要删除的方案分类主键
     * @return 结果
     */
    @Override
    public int deletecategoriesByIds(Long[] ids) {
        checkCategoryHasSolutions(ids);
        return categoriesMapper.deleteCategoriesByIds(ids);
    }
    /**
     * 删除方案分类信息
     *
     * @param id 方案分类主键
     * @return 结果
     */
    @Override
    public int deletecategoriesById(Long id) {
        return categoriesMapper.deleteCategoriesById(id);
    }

    private void checkCategoryHasSolutions(Long[] ids) {
        List<Categories> categoriesList= solutionCategoryMapper.selectByCategoryIds(ids);
        if(categoriesList.size()>0){
            throw new BaseException("该分类下有方案，无法执行操作");
        }
    }
}
